package com.ayuku.commons;

import java.math.BigDecimal;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class UtilServer {

	public static String SESSION_ID;

	public static String cleanStr(String str) {
		return str.replace("'", "");
	}

	public static final SimpleDateFormat TANGGAL_QUERY_DB = new SimpleDateFormat(
			"yyyy-MM-dd", Locale.US);

	public static String nullToString(String str) {
		return (str == null) ? "" : str.trim();
	}

	public static Object nullToNumber(final Object obj) {
		return (obj == null) ? "0.00" : obj;
	}

	public static boolean isEmpty(final String str) {
		boolean rtrn = false;

		if ((str == null) || str.trim().equals("")) {
			rtrn = true;
		}

		return rtrn;
	}

	public static void doGarbage() {
		final Runtime runti = Runtime.getRuntime();
		runti.gc();
	}

	public static final SimpleDateFormat TANGGAL_DB = new SimpleDateFormat(
			"yyyyMMdd", Locale.US);

	public static Date strToDate(String arg) {
		if (arg == null || "".equals(arg.trim())) {
			return null;
		}
		Date date = null;
		try {
			date = (Date) TANGGAL_DB.parse(arg);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return date;

	}

	public static Integer strToInt(String arg) {
		if (arg == null || "".equals(arg.trim())) {
			return null;
		}
		int in = 0;
		try {
			in = Integer.parseInt(arg.trim());
		} catch (Exception e) {

		}
		return Integer.valueOf(in);
	}

	public static BigDecimal strToBigDecimal(String arg) {
		if (arg == null || "".equals(arg.trim())) {
			return null;
		} else {
			Pattern p = Pattern.compile("([0-9]*)\\.([0-9]*)");
			Matcher m = p.matcher(arg.trim());
			if (m.matches()) {
				return new BigDecimal(arg.trim());
			} else {
				return null;
			}
		}
	}

	public static Double strToDouble(String arg) {
		if (arg == null || "".equals(arg.trim())) {
			return null;
		} else {
			return Double.valueOf(arg.trim());
		}
	}

	public static boolean strToBoolean(String arg) {
		if (arg == null || "".equals(arg.trim())) {
			return false;
		} else {

			if (arg.trim().equalsIgnoreCase("False")
					|| arg.trim().equalsIgnoreCase("F")) {
				return false;
			} else if (arg.trim().equalsIgnoreCase("true")
					|| arg.trim().equalsIgnoreCase("t")) {
				return true;
			}
		}
		return false;
	}

	public static Date getCurrentDate() throws Exception {
		Calendar cal = new GregorianCalendar();
		return cal.getTime();
	}



	public static long getDaysBtwTwoDate(Date start, Date end) {
		Calendar startDate = Calendar.getInstance();
		startDate.setTime(start);
		Calendar endDate = Calendar.getInstance();
		endDate.setTime(end);
		return daysBetween(startDate, endDate);
	}

	/** Using Calendar - THE CORRECT WAY **/
	public static long daysBetween(Calendar startDate, Calendar endDate) {
		Calendar date = (Calendar) startDate.clone();
		long daysBetween = 0;
		while (date.before(endDate)) {
			date.add(Calendar.DAY_OF_MONTH, 1);
			daysBetween++;
		}
		return daysBetween;
	}

	public static String getMd5(String pwd) throws NoSuchAlgorithmException {
		if (pwd==null){
			return null;
		}
		MessageDigest algorithm = MessageDigest.getInstance("MD5");
		algorithm.reset();

		byte[] defaultBytes = pwd.getBytes();

		algorithm.update(defaultBytes);
		byte messageDigest[] = algorithm.digest();

		StringBuffer hexString = new StringBuffer();
		for (int i = 0; i < messageDigest.length; i++) {
			hexString.append(Integer.toHexString(0xFF & messageDigest[i]));
		}

		return hexString.toString();
	}

}
